// 收到5开头的消息
import { Message } from 'element-ui'
import { resArr } from '../socketOnMessageTools'
import { decryptMessage } from '../socketTools'
import store from '@/store'
import { sendSock } from '@/utils/socketSend'
import { aesDecrypt } from '@/secret/secret'
import tools from '@/utils/tools'
import router from '@/router'

const dataDealer = tools.dataDealer

// 解析消息
const decryptMsg = decryptMessage

export default async (type, result) => {
  switch (type) {
    // 密钥交换
    case '5.1.1': {
      store.state.socket.globalCallback(result)

      // 密钥交换完成之后立即发起身份认证
      sendSock({
        type: '5.1.2',
        body: {
          token: store.getters.token,
          deviceType: 3
        }
      })
      store.commit('socket/set_operationReqId', 'identityReqId')
      break
    }
    // 心跳
    case '5.1.3': {
      break
    }
    // 强制下线
    case '5.1.4': {
      const reasonData = aesDecrypt(result.body, store.state.socket.DHSecretKey, true)
      const reason = dataDealer.uint8ToNum(reasonData, 8)
      Message({
        type: 'error',
        message: reason === 1 ? '密码修改，请重新登录' : '账号在其他设备登录'
      })
      router.push('/login')
      break
    }
    // 某人撤回了一条消息
    case '5.2.6': {
      decryptMsg(result.body, ['id_type', 'id', 'msg_id'], 0)
      const realData = resArr[0]
      // 撤回消息
      store.commit('chatInformation/withdrawMsg', realData)
      break
    }
  }
}
